/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-01-27
 * Time: 10:44
 */
//ListNode 代表一个结点
class ListNode{
    public  int value;
    //next存放下一个结点的地址，所以定义它的类型为ListNode(结点类型)
    public ListNode next;

    public ListNode(int value){
        this.value = value;
    }
}
public class MyList {
    //head 指代MyList这个链表的头引用(指向第一个结点)所以定义在链表中
    public ListNode head; //链表的头引用
    public void createList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }
    public void display(){
        ListNode cur = this.head; //定义一个变量cur指向head
        //结点中next域的值为null 代表是最后一个结点
        while (cur!=null){
            System.out.print(cur.value+" ");
            //将写一个结点的地址赋给cur，cur就会走到下一个结点
            cur = cur.next;
        }
        System.out.println();
    }
    //从指定头结点开始进行打印
    public void display2(ListNode newHead){
        ListNode cur = newHead;
        while (cur!=null){
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur!=null){
            if(cur.value==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while (cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        ListNode Node = new ListNode(data);
        Node.next = this.head;
        this.head = Node;

        //分开的写法
       /* if(this.head==null){
            this.head = Node;
        }else {
            Node.next = this.head;
            this.head = Node;
        }*/
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
       if(this.head==null){
           this.head = node;
       }else {
           ListNode cur = this.head;
          while (cur.next!=null){
              cur = cur.next;
          }
          //循环结束后 cur.next = null
          cur.next = node;
       }
    }
    //找到index-1的位置
    public ListNode findIndex(int index){
        ListNode cur = this.head;
        while (index-1!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index<0 || index >size()){
            System.out.println("index位置不合法！");
            return;
        }
        if(index ==0 ){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }

    //找待删除结点的前驱
    public ListNode findPreNode(int key){
        ListNode cur = this.head;
        while (cur.next!=null){
            if(cur.next.value==key){
                return cur;
            }
            cur =cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head==null){
            System.out.println("链表为空");
            return;
        }
        if(this.head.value == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPreNode(key);
        if(cur==null){
            System.out.println("没有要删除的结点");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head==null){
            System.out.println("链表为空");
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur!=null){
            if(cur.value==key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
            //最后处理头结点
            if(this.head.value==key){
                this.head = this.head.next;
            }
        }
    }

    public void clear(){
        //this.head = null;
        while (this.head!=null){
            ListNode curNext = this.head.next;
            this.head = null;
            this.head = curNext;
        }
    }

    //链表逆置
    public ListNode reverList(){
        if(this.head==null){
            return null;
        }
        ListNode cur = this.head;
        ListNode prev = null;
        while (cur!=null){
            ListNode curNext = cur.next;
            cur.next = prev;
            prev =cur;
            cur = curNext;
        }
        return prev;
    }

    //判断中间结点的位置
    public ListNode middleNod (){
        if(head == null){
            return null;
        }
        ListNode first = head;
        ListNode slow = head;
        while(first!=null && first.next!=null){
            first = first.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //判断倒数第K个结点
        public ListNode FindKthToTail(ListNode head,int k) {
            if(k<=0 || head==null){
                return null;
            }
            ListNode fast = head;
            ListNode slow = head;
            while(k-1!=0){
                fast = fast.next;
                if(fast == null){
                    return null;
                }
                k--;
            }
            while(fast.next!=null){
                fast = fast.next;
                slow = slow.next;
            }
            return slow;
        }

    //合并两个单链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1==null && list2==null){
            return null;
        }
        ListNode newHead= new ListNode(-1);
        ListNode tmp = newHead;
        while(list1!=null && list2!=null){
            if(list1.value <= list2.value){
                tmp.next = list1;
                list1 = list1.next;
                tmp = tmp.next;
            }else{
                tmp.next = list2;
                list2  = list2.next;
                tmp = tmp.next;
            }
        }
        if(list1!=null){
            tmp.next = list1;
        }
        if(list2!=null){
            tmp.next = list2;
        }
        return newHead.next;
    }

    //以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前
    public ListNode partition(int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while (cur!=null) {
            if(cur.value <x){
                //第一次
                if(bs==null){
                    bs = cur;
                    be = cur;
               //不是第一次，采用尾插
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                //第一次
                if(as == null){
                    as = cur;
                    ae = cur;
               //不是第一次，采用尾插
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //如果所有的数据都大于x，返回大于x的区间
        if(bs==null){
            return as;
        }
        //连接两段区间
        be.next = as;
        //如果原链表最后一个结点的next域不为空，要将其置为null
        if(as!=null){
            ae.next = null;
        }
        return bs;
    }

    //删除链表中重复的结点
    public ListNode deleteDuplication() {
        ListNode cur = head;
        //创建一个临时结点，没有实际意义，所以赋值为-1
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (cur!=null){
            //相邻结点之间的数据相同
            if(cur.next!=null && cur.value == cur.next.value){
                while (cur.next!=null && cur.value == cur.next.value){
                    cur = cur.next;
                }
                //将相同结点遍历完后，cur 还要往后走一次
                cur = cur.next;
                //相邻结点之间的数据不相同
            }else {
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        //如果最终tmp所指结点的next域不是null，就要赋值为null
        tmp.next = null;
        return newHead.next;
    }

    //判断是否是回文链表
    public boolean chkPalindrome() {
        // write code here
        ListNode fast = head;
        ListNode slow = head;
        //找中间结点
        while (fast!=null && fast.next!=null){
            fast =fast.next.next;
            slow = slow.next;
        }
        //链表逆置
        ListNode cur = slow.next;
        while (cur!=null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //判断回文
        while (head!=slow){
            if(head.value != slow.value){
                return false;
            }
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //构造相交结点
    public void createRing(ListNode headA, ListNode headB){
        headA.next.next = headB.next.next;
    }

    // 输入两个链表，找出它们的第一个公共结点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null || headB==null){
            return null;
        }
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        while(pl!=null){
            lenA++;
            pl = pl.next;
        }
        pl = headA;
        while(ps!=null){
            lenB++;
            ps = ps.next;
        }
        ps = headB;
        //求差值
        int len = lenA - lenB;
        if(len<0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //pl指向了最长的链表，ps指向了最短的链表，len为差值步
        //让pl先走len步
        while(len>0){
            pl = pl.next;
            len--;
        }
        //pl 与 ps 同时走，当pl与pl相遇就结束
        while(pl!=ps){
            pl = pl.next;
            ps = ps.next;
            if(pl==null || ps==null){
                return null;
            }
        }
        return pl;
    }

    public void createPoot(){
        ListNode cur = this.head;
        while (cur.next!=null){
            cur = cur.next;
        }
        cur.next  = head.next.next;
    }
    //判断链表是否有环
    public boolean hasCycle() {
        if(head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while(fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
    //第二种写法
    public boolean hasCycle2() {
        if(head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return false;
        }
        return true;
    }

    //给定一个链表，返回链表开始入环的第一个节点。 如果链表无环，则返回 null
    public ListNode detectCycle() {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(fast!=null && fast.next!= null){
            fast = fast.next.next;
            slow =slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast ==null || fast.next==null){
            return null;
        }
        fast = head;
        while(fast!=slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

